package com.myself.community.service.impl;

import com.myself.community.dao.MessageMapper;
import com.myself.community.entity.Message;
import com.myself.community.service.MessageService;
import com.myself.community.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    /**
     *根据用户id，查询当前用户的会话列表,针对每个会话只返回一条最新的私信.
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> findConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId, offset, limit);
    }

    /**
     *查询当前用户的会话数量.
     * @param userId
     * @return
     */
    @Override
    public int findConversationCount(int userId) {
        return messageMapper.selectConversationCount(userId);
    }

    /**
     *查询某个会话所包含的私信列表.
     * @param conversationId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }

    /**
     *查询某个会话所包含的私信数量.
     * @param conversationId
     * @return
     */
    @Override
    public int findLetterCount(String conversationId) {
        return messageMapper.selectLetterCount(conversationId);
    }

    /**
     *查询未读私信的数量
     * @param userId
     * @param conversationId
     * @return
     */
    @Override
    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    /**
     *新增消息
     * @param message
     * @return
     */
    @Override
    public int addMessage(Message message) {
        //HTML标签过滤
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        //敏感词过滤
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    /**
     *修改消息的状态:将私信信息设置为已读
     * @param ids
     * @return
     */
    @Override
    public int updateMessageStatus(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }

    /**
     * 查询某主题的最新的系统通知
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }

    /**
     * 查询某主题下的所有通知数量
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public int findNoticeCount(int userId, String topic) {
        return messageMapper.selectNoticeCount(userId, topic);
    }

    /**
     * 查询某主题下未读消息数量：若不传入主题，则查询当前用户的所有未读系统通知数量
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    /**
     * 查询某个主题所包含的通知列表
     * @param userId
     * @param topic
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }
}
